package org.linuxsogood.boot.lock;

/**
 * @author honway.liu
 * @date 2019-07-31 15:34
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class Lock {
    private boolean isLocked;

    public synchronized void lock() throws InterruptedException {
        while (isLocked) {
            wait();
        }
        isLocked = true;
    }

    public synchronized void unLock() {
        isLocked = false;
        notify();
    }
}

class LockTest {
    static Lock lock = new Lock();
    public static void main(String[] args) throws InterruptedException {
        lock.lock();
        addMethod();
        lock.unLock();
    }

    private static void addMethod() throws InterruptedException {
        System.out.println("invocation add method");
        lock.lock();
        otherMethod();
        lock.unLock();
    }

    private static void otherMethod() {
        System.out.println("other method invocation....");
    }
}